package com.hksj.Intelligentmanagement.auth.config;

import com.hksj.Intelligentmanagement.auth.filter.JwtAuthenticationTokenFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;


/**
 * @description:加密的类
 * @return 1:String encode1 = bCryptPasswordEncoder.encode("123456");对密码进行加密
 */
@Configuration
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    private JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;

    @Autowired
    private AuthenticationEntryPoint authenticationEntryPoint;

    @Autowired
    private AccessDeniedHandler accessDeniedHandler;

    @Bean//创建BCryptPasswordEncoder注入容器（作用:密码加密和校验）
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }

    @Bean//将AuthenticationManager注入容器中、以供登录的时候入口验证使用
    @Override
    protected AuthenticationManager authenticationManager() throws Exception {
        return super.authenticationManager();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                //关闭csrf
                .csrf().disable()
                //不通过Session获取SecurityContext 前后端分离 session不管用的
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                .and()
                .authorizeRequests()
                //放开静态资源
//                .antMatchers("/api/surgical/exportSchedulingSurgical").permitAll()
                // 对于登录接口 允许匿名访问   {不携带token能访问，但是携带token不能访问}
                .antMatchers("/api/login/doLogin").anonymous()
                //权限认证
                //.antMatchers("/api/*").hasAuthority("system:dept:list")
                //.antMatchers("/api/**").anonymous()
                .antMatchers("/mqtt/**").anonymous()
                .antMatchers("/resource/process").anonymous()
                .antMatchers("/api/readHandNumbness/**").anonymous()
                .antMatchers("/api/hqEnvironmentHistoryData/syncData").anonymous()
                .antMatchers("/api/hqEnvironmentHistoryData/enableAutoSync/*").anonymous()
                // 除上面外的所有请求全部需要鉴权认证，任意用户认证都可以访问
                .anyRequest().authenticated();


        //把token校验过滤器添加到过滤器链中
        http.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class);
        //配置异常处理器
        http.exceptionHandling().authenticationEntryPoint(authenticationEntryPoint)
                .accessDeniedHandler(accessDeniedHandler);

        //允许跨域
        http.cors();
    }


    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring().antMatchers("/swagger-ui.html")
                .antMatchers("/swagger-ui/*")
                .antMatchers("/swagger-resources/**")
                .antMatchers("/v2/api-docs").
                antMatchers("/v3/api-docs").
                antMatchers("/webjars/**").
                antMatchers("/doc.html");
    }

}